﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Drawing;

using MyLibrary.UI.Properties;


namespace MyLibrary.UI.HighLight
{
    public enum LanguageType
    {
        CSharp,
        SQL
    }

    public class Parser
    {
        private XmlDocument doc;    //存放关键字的XML文件
        private Dictionary<string, System.Drawing.Color> keyWordList = null;   //关键字列表
        private bool caseSensitive = false; //大小写敏感
        private static Parser parser = null;

        public static Parser CreateParserInstance(LanguageType language)
        {
            if (parser == null)
                parser = new Parser(language);
            return parser;
        }

        protected Parser(LanguageType language)
        {
            this.LoadKeyWord(language);
        }

        /// <summary>
        /// 获取KeyWord列表
        /// </summary>
        /// <param name="language">语言类型</param>
        private void LoadKeyWord(LanguageType language)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string fileName = String.Empty;

            switch (language)
            {
                case LanguageType.CSharp:
                    fileName = "csharp.xml";
                    break;
                case LanguageType.SQL:
                    fileName = "sql.xml";
                    break;
                default:
                    break;
            }

            //Stream stream = new MemoryStream(Convert.ToByte(Resources.csharp));
            Stream stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".Resources." + fileName);   //获取XML数据流
            StreamReader reader = new StreamReader(stream);

            doc = new XmlDocument();
            doc.Load(reader);

            keyWordList = new Dictionary<string, Color>();
            XmlElement root = doc.DocumentElement;
            XmlNodeList nList = root.SelectNodes("/definition/keyword");
            foreach (XmlNode node in nList)
            {
                if (node.Attributes["color"].Value == "blue")
                    keyWordList.Add(node.InnerText, Color.Blue);
                else if (node.Attributes["color"].Value == "purple")
                    keyWordList.Add(node.InnerText, Color.Purple);
            }
            Boolean.TryParse(root.Attributes["caseSensitive"].Value, out this.caseSensitive);
        }

        /// <summary>
        /// 判断是否为关键字
        /// </summary>
        public bool IsKeyWord(string word,out Color wordColor)
        {
            wordColor = Color.Black;
            //如果区分大小写
            if (caseSensitive)
            {
                if (keyWordList.ContainsKey(word))
                {
                    wordColor = keyWordList[word];
                    return true;
                }
                return false;
            }
            //不区分大小写
            else
            {
                if (keyWordList.ContainsKey(word.ToLower()))
                {
                    wordColor = keyWordList[word.ToLower()];
                    return true;
                }
                return false;
            }
        }
    }
}
